package com.chl.jdk8.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class CollectMapDemo {

	static int n = 100;
	static List<Persion> persions = new ArrayList<>(n);
	
	@BeforeAll
	static void setUpBeforeClass() throws Exception {
		Persion p;
		Random r = new Random();
		for (int i = 0; i < n; i++) {
			p = new Persion();
			p.setAge(10 + r.nextInt(3));
			p.setGender(r.nextInt(10));
			p.setName("name" + i);
			persions.add(p);
		}
	}

	@Test
	void toMap() {
		Map<String, Integer> ageMap = persions.stream().collect(Collectors.toMap(Persion::getName, Persion::getAge));
		System.out.println(ageMap);
		
		Map<String, Persion> persionMap = persions.stream().collect(Collectors.toMap(Persion::getName, (Persion p) -> p));
		System.out.println(persionMap);
	}

}
